package edu.pw.p2pclient.activities;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import edu.pw.p2pclient.R;
import edu.pw.p2pclient.data.DirectoryTreeAdapter;
import edu.pw.p2pclient.data.Node;
import edu.pw.p2pclient.data.TreeNode;
import edu.pw.p2pclient.logic.SharedManager;
import edu.pw.p2pclient.utils.ApplicationContext;

public class AddToSharedActivity extends Activity {

	private ListView mListView;
	private DirectoryTreeAdapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		ApplicationContext.init(getApplicationContext());
		setContentView(R.layout.activity_add_to_shared);

		mListView = (ListView) findViewById(R.id.addFilesListView);
		mAdapter = new DirectoryTreeAdapter(this, mListView, true);
		mListView.setAdapter(mAdapter);
		registerForContextMenu(mListView);
		mAdapter.showDirectory(TreeNode.ROOT_PATH);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		getMenuInflater().inflate(R.menu.activity_add_to_shared, menu);
		return true;
	}

	@Override
	public void onBackPressed() {

		if (mAdapter.getCurrentFolder().equals(TreeNode.ROOT_PATH))
			super.onBackPressed();
		else
			mAdapter.showDirectory(mAdapter.getParentFolder());
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.shareItem:
			if (!mAdapter.isAnySelected()) {
				Toast.makeText(getApplicationContext(),
						R.string.you_must_check_item_first, Toast.LENGTH_SHORT)
						.show();
			} else {
				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				View newView = getLayoutInflater().inflate(
						R.layout.sharing_interfaces_dialog, null);
				LinearLayout layout = (LinearLayout) newView
						.findViewById(R.id.checkboxRowLayout);
				LinearLayout layout2 = (LinearLayout) newView
						.findViewById(R.id.passwordEditLayout);

				TextView title = (TextView) newView
						.findViewById(R.id.titleTextView);
				title.setText("Wybierz interfejsy:");
				final ToggleButton img1 = (ToggleButton) newView
						.findViewById(R.id.toggleButton1);
				final ToggleButton img2 = (ToggleButton) newView
						.findViewById(R.id.toggleButton2);
				final ToggleButton img3 = (ToggleButton) newView
						.findViewById(R.id.toggleButton3);
				final EditText pass = new EditText(newView.getContext());
				pass.setInputType(InputType.TYPE_CLASS_TEXT
						| InputType.TYPE_TEXT_VARIATION_PASSWORD);
				pass.setEnabled(false);
				pass.setFocusable(false);
				LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
						LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
				params.weight = 1.0f;
				pass.setLayoutParams(params);
				final CheckBox checkbox = new CheckBox(newView.getContext());
				checkbox.setText(R.string.resources_password_protected);
				checkbox.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						pass.setEnabled(checkbox.isChecked());
						if (checkbox.isChecked())
							pass.setFocusableInTouchMode(true);
						else
							pass.setFocusable(false);
					}
				});
				img1.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						img3.setChecked(false);
						checkbox.setEnabled(true);
					}
				});
				img2.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						img3.setChecked(false);
						checkbox.setEnabled(true);
					}
				});
				img3.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						if (img3.isChecked()) {
							img1.setChecked(false);
							img2.setChecked(false);
							checkbox.setChecked(false);
							checkbox.setEnabled(false);
							pass.setEnabled(false);
							pass.setFocusable(false);
						} else {
							checkbox.setEnabled(true);
							pass.setFocusable(true);
						}
					}
				});
				layout.addView(checkbox);
				layout2.addView(pass);
				builder.setView(newView)
						.setNegativeButton(R.string.cancel,
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int id) {
										mAdapter.deselectItems();
										dialog.cancel();
									}
								})
						.setPositiveButton(android.R.string.ok,
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int id) {
										ArrayList<Node> items = mAdapter
												.getSelectedItems();
										if (!checkbox.isChecked())
											pass.setText("");
										SharedManager.getInstance()
												.addToShared(
														items,
														img1.isChecked(),
														img2.isChecked(),
														img3.isChecked(),
														pass.getText()
																.toString());
										if (!img1.isChecked()
												&& !img2.isChecked()
												&& !img3.isChecked()) {
											Toast.makeText(
													getApplicationContext(),
													R.string.selected_resources_are_not_shared_anymore,
													Toast.LENGTH_SHORT).show();
										}
										mAdapter.deselectItems();
										mAdapter.notifyDataSetChanged();
									}
								});
				builder.show();
			}
			break;
		case R.id.dontShareItem:
			if (!mAdapter.isAnySelected()) {
				Toast.makeText(getApplicationContext(),
						R.string.you_must_check_item_first, Toast.LENGTH_SHORT)
						.show();
			} else {
				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				builder.setTitle(R.string.confirmation);
				builder.setMessage(R.string.do_you_want_to_stop_sharing)
						.setNegativeButton(R.string.cancel,
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										dialog.cancel();
									}
								})
						.setPositiveButton(android.R.string.ok,
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int id) {

										SharedManager
												.getInstance()
												.removeFromShared(
														mAdapter.getSelectedItems());
										mAdapter.deselectItems();
										mAdapter.notifyDataSetChanged();
									}
								});
				builder.show();
			}
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {

		super.onCreateContextMenu(menu, v, menuInfo);
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
		Node node = mAdapter.getItem(info.position);
		if (node.isMoveUp())
			return;
		if (!node.isShared()) {
			Toast.makeText(getApplicationContext(),
					R.string.element_is_not_shared, Toast.LENGTH_SHORT).show();
			return;
		}
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		View newView = getLayoutInflater().inflate(
				R.layout.sharing_interfaces_dialog, null);
		builder.setView(newView);

		TextView title = (TextView) newView.findViewById(R.id.titleTextView);
		ToggleButton img = (ToggleButton) newView
				.findViewById(R.id.toggleButton1);
		ToggleButton img2 = (ToggleButton) newView
				.findViewById(R.id.toggleButton2);

		title.setText(R.string.element_shared_via);
		img.setClickable(false);
		img2.setClickable(false);
		img.setChecked(node.isBtShared());
		img2.setChecked(node.isWifiShared());

		AlertDialog dialog = builder.create();
		dialog.setCanceledOnTouchOutside(true);
		dialog.show();
	}

}
